<?php
/**
 * 支付类
 *
 * @author 
 */
namespace Common\qscmslib;
class pay
{
    protected $_error;
    protected $_setting = array();
    protected $_type;
    protected $_payFrom = array('pc','app','wap','wx_mini');
    public function __construct($name) {
        $this->_type = $name ? $name : 'alipay'; //C('qscms_pay_default_type')
        $pay_type = D('Payment')->get_cache();
        $this->_setting = $pay_type[$this->_type];
        include QSCMSLIB_PATH . "pay/{$this->_type}/{$this->_type}.class.php";
        $class = $this->_type . '_pay';
        $this->_pay = new $class($this->_setting);
     //   var_dump($class);die;
    }
    /*
        $option
        oid 订单号
        ordsubject 订单名称
        ordtotal_fee 订单金额
        ordbody 订单描述
        site_dir 网站域名
    */
    public function cash_with_order($type='pc',$option){
    	if(!in_array($type,$this->_payFrom)){
    		$this->_error = '请选择正确的付款途径！';
    		return false;
    	}
        $option['pay_from'] = $type;
        if(!$option['site_dir'])
        {
            $this->_error = '请填回调前缀！';
            return false;
        }
        if(!$option['oid'])
        {
            $this->_error = '请填写订单号！';
            return false;
        }
        if(!$option['oid'])
        {
            $this->_error = '订单号错误！';
            return false;
        }
        if(!$option['ordsubject'])
        {
            $this->_error = '请填写订单名称！';
            return false;
        }
        if(!$option['ordtotal_fee'])
        {
            $this->_error = '请填订单金额！';
            return false;
        }
        if(!$option['ordbody'])
        {
            $this->_error = '请填写订单描述！';
            return false;
        }
        $result = $this->_pay->dopay($option);
//        var_dump($result);
//        var_dump($option);die;
    	// if(false === $result = $this->_sms->sendTemplateSMS($type,array('mobile'=>$option['mobile'],'content'=>$content))){
    	// 	$this->_error = $this->_sms->getError();
    	// 	return false;
    	// }
        if($result === false) $this->_error = $this->_pay->getError(); 
    	return $result;
    }

    /*
        $option
        oid 订单号
        ordsubject 订单名称
        ordtotal_fee 订单金额
        ordbody 订单描述
        site_dir 网站域名
        LD  新增
    */
    public function wx_cash_with_order($type='pc',$option){
        if(!in_array($type,$this->_payFrom)){
            $this->_error = '请选择正确的付款途径！';
            return false;
        }
        $option['pay_from'] = $type;
        if(!$option['site_dir'])
        {
            $this->_error = '请填回调前缀！';
            return false;
        }
        if(!$option['oid'])
        {
            $this->_error = '请填写订单号！';
            return false;
        }
        if(!$option['oid'])
        {
            $this->_error = '订单号错误！';
            return false;
        }
        if(!$option['ordsubject'])
        {
            $this->_error = '请填写订单名称！';
            return false;
        }
        if(!$option['ordtotal_fee'])
        {
            $this->_error = '请填订单金额！';
            return false;
        }
        if(!$option['ordbody'])
        {
            $this->_error = '请填写订单描述！';
            return false;
        }
        //$result = $this->_pay->_pay_from_app($option);
        if($option['payFrom']=='pc'){
       //     var_dump(111);die;
            $result = $this->_pay->dopay($option);
        }else{
            $result = $this->_pay->_pay_from_wxxcx($option);
        }
         //  var_dump($result);die;

        // if(false === $result = $this->_sms->sendTemplateSMS($type,array('mobile'=>$option['mobile'],'content'=>$content))){
        // 	$this->_error = $this->_sms->getError();
        // 	return false;
        // }
        if($result === false) $this->_error = $this->_pay->getError();
        return $result;
    }

    /*
        $option
        oid 原支付订单号
        r_oid 退款订单号
        ordtotal_fee 订单金额
        ordbody 订单描述
        site_dir 网站域名
        LD  新增
    */
    public function wx_refund_order($option){
        if(!$option['oid'])
        {
            $this->_error = '原支付订单号错误！';
            return false;
        }

        if(!$option['r_oid'])
        {
            $this->_error = '退款订单号错误！';
            return false;
        }

        if(!$option['amount'])
        {
            $this->_error = '请填订单支付金额！';
            return false;
        }

        if(!$option['ordtotal_fee'])
        {
            $this->_error = '请填订单退款金额！';
            return false;
        }

        $result = $this->_pay->pay_refund($option);
        if($result === false) $this->_error = $this->_pay->getError();
        return $result;
    }

    public function alipayNotify(){
        return $this->_pay->alipayNotify();
    }
    public function alipayNotifyReturn(){
        return $this->_pay->alipayNotifyReturn();
    }
    /**
     * [payment 企业付款]
     */
    public function payment($data){
        if(!$data['openid']){
            $this->_error = '请填写用户openid！';
            //	var_dump($this->_error);die;
            return false;
        }
        if(!$data['partner_trade_no']){
            $this->_error = '请填写订单号！';
            return false;
        }
        if($data['amount'] <= 0){
            $this->_error = '请填写订单金额！';
            return false;
        }
        $pay = new \MchPay();
        // 用户openid
        //$data['openid']='oy2lbszXkgvlEKThrzqEziKEBzqU';
        // 商户订单号
        //$data['partner_trade_no']='test-'.time();
        // 校验用户姓名选项
        $data['check_name'] = 'NO_CHECK';
        // 企业付款金额  单位为分
        $data['amount'] = $data['amount'] * 100;
        // 企业付款描述信息
        $data['desc']='红包发放';
        // 调用接口的机器IP地址  自定义
        $data['spbill_create_ip'] = get_client_ip();
        // 收款用户姓名
        // $data['re_user_name']='Max wen';
        // 设备信息
        // $data['device_info']='dev_server';
        if(false !== $reg = $pay->postXmlSSL($data)) return $reg;
        $this->_error = $pay->getError();
        return false;
       // return $this->_pay->payment($data);
    }
    public function getError() {
        return $this->_error;
    }
}